Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Histogram Max Rectangular Area

Hard Acceptance 32.12% Points 40.00

You are given a histogram represented by an array arr, where each element represents the height of a bar, and all bars have a uniform width of 1 unit. Your task is to determine the largest rectangular area that can be formed using one or more consecutive bars in the histogram.

Examples
Example 1

Example 1

Input: arr[] = [6, 2, 5, 4, 5, 1, 6]

Output: 12

Explanation: The largest rectangle has a height of 4 and width of 3, giving an area of 4 × 3 = 12. This is formed by choosing the 3rd, 4th, and 5th bars.

Example 2

Example 2

Input: arr[] = [2, 4, 6, 3]

Output: 9

Explanation: The largest rectangle has a height of 3 and width of 3, giving an area of 3 × 3 = 9. This is formed by choosing the 2nd, 3rd, and 4th bars.

Hints
Hint 1
Expected Time Complexity: O(n)
Hint 2
Expected Auxiliary Space: O(1)
Constraints
  • 1 <= arr.size() <= 10^5
  • 0 <= arr[i] <= 10^3
Companies
Microsoft Google
Topics
Stack
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions